<?php
App::uses('AppShell', 'Console/Command');
App::uses('App', 'Controller');
App::uses('SmsComponent', 'Controller/Component');

class ServiceReminderShell extends AppShell {
    var $uses = array('CustomerAppointment','Customer','Config');
    var $remind_limit = 1;

    var $service_advisors = array('233266352282');

    function main() {
        // run if no action is passed
        //$this->out('Hello world.');

        /* ERROR CODES FOR SMS */
       /* 1701: Success, Message Submitted Successfully, In this case you will receive the response 1701|<CELL_NO>:<MESSAGE ID>, The message Id can then be used later to map the delivery reports to this message.
        1702: Invalid URL Error, This means that one of the parameters was not provided or left blank
        1703: Invalid value in username or password field
        1704: Invalid value in "type" field
        1705: Invalid Message
        1706: Invalid Destination //Cell fon
        1707: Invalid Source (Sender)
        1708: Invalid value for "dlr" field
        1709: User validation failed
        1710: Internal Error
        1025 :Insufficient Credit*/
    }

    function fire(){
        $this->remind();
        $this->birthday_wishes();
    }

    function remind() {
        $res = $this->CustomerAppointment->today_reminders_count('service_customer',false);
        //debug($res);
        $sms =& new SmsComponent(new ComponentCollection());
        $customers = array();
        $update_reminders = array();
        if($res['count'] > 0){
            foreach($res['data'] as $arr){
                $id = $arr['id'];
                $fon = $arr['telephone'];
                $name = $arr['name'];
                $info = $arr['next_service_date'];
                $vehicle_number = $arr['vehicle_number'];
                $reminder_count = $arr['reminder_count'];
                if($reminder_count < $this->remind_limit){// Apply Sms remind limit
                    $msg = "The Management of Plant Pool Ltd. will like to inform you that your next servicing on $vehicle_number  will be on ".$info.". Thank you for your corperation.";
                    $res = $sms->sendSMS($msg, $fon);
                   // $res = "1701|9300499590";
                    if($res){
                        $explode_arr = explode('|',$res);
                        $status_code = trim($explode_arr[0]);
                        if($status_code == 1701){
                            $customers[] = $name;
                            $update_reminders[] =$id;
                        }
                    }
                }
            }
            //Notify the service advisors
            if(count($customers) > 0){ //If customers have been notified
                $name_str = '';
                foreach($customers as $name){
                    $name_str .= $name.',';
                }
                $msg = 'Dear Staff Member, this is to inform you that '.$name_str.' have been reminded on their next servicing schedules';
                foreach($this->service_advisors as $fon){
                    $sms->sendSMS($msg, $fon);
                   // debug($msg);
                }
            }

            //update those reminders
            if(count($update_reminders) > 0){
                $save = array();
                 $reminders_data = $this->CustomerAppointment->getRemindersById($update_reminders);
                foreach($reminders_data as $r_data){
                    $r = $r_data['CustomerAppointment'];
                    $save[]=array(
                        'id'=>$r['id'],
                        'reminded'=>'Reminded',
                        'reminder_count'=>$r['reminder_count'] + 1
                    );
                }
                $this->CustomerAppointment->saveAll($save);
            }


        }
    }


    function birthday_wishes() {
        $res = $this->Customer->getBirthdayCustomers('service_customer');
        $config = $this->Config->getConfigByKey('service_ad_dob_msg');
        $defualt_msg = trim($config['Config']['value']);

        $sms =& new SmsComponent(null);
        $update_customers = array();
        if(count($res) > 0){
            foreach($res as $customer){
                $arr  = $customer['Customer'];
                $id = $arr['id'];
                $fon = $arr['telephone'];
                $name = $arr['name'];
                $dob_msg = trim($arr['dob_msg']);
                $msg = $defualt_msg;
                if(!empty($dob_msg)){
                    $msg = $dob_msg;
                }

                $res_sms = $sms->sendSMS($msg, $fon);
                if($res_sms){
                    $explode_arr = explode('|',$res_sms);
                    $status_code = trim($explode_arr[0]);
                    if($status_code == 1701){
                        $update_customers[] = $id;
                    }
                }
            }

            //update those customers wishes to yes
            if(count($update_customers) > 0){
                $this->Customer->updateCustomerBirthdayWished($update_customers,'Yes');
            }
        }
    }
}